﻿using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

namespace FileManage;

/// <summary>
/// image帮助类
/// </summary>
public static class ImageHelper
{
    /// <summary>
    /// 适配缩量尺寸
    /// </summary>
    /// <param name="spcWidth"></param>
    /// <param name="spcHeight"></param>
    /// <param name="orgWidth"></param>
    /// <param name="orgHeight"></param>
    /// <returns></returns>
    public static Size AdjustSize(int spcWidth, int spcHeight, int orgWidth, int orgHeight)
    {
        Size size = new Size();
        // 原始宽高在指定宽高范围内，不作任何处理 
        if (orgWidth <= spcWidth && orgHeight <= spcHeight)
        {
            size.Width = orgWidth;
            size.Height = orgHeight;
        }
        else
        {
            // 取得比例系数 
            float w = orgWidth / (float)spcWidth;
            float h = orgHeight / (float)spcHeight;
            // 宽度比大于高度比 
            if (w > h)
            {
                size.Width = spcWidth;
                size.Height = (int)(w >= 1 ? Math.Round(orgHeight / w) : Math.Round(orgHeight * w));
            }
            // 宽度比小于高度比 
            else if (w < h)
            {
                size.Height = spcHeight;
                size.Width = (int)(h >= 1 ? Math.Round(orgWidth / h) : Math.Round(orgWidth * h));
            }
            // 宽度比等于高度比 
            else
            {
                size.Width = spcWidth;
                size.Height = spcHeight;
            }
        }

        return size;
    }

    /// <summary>
    /// 获取缩略图
    /// </summary>
    /// <param name="image"></param>
    /// <param name="spcWidth"></param>
    /// <param name="spcHeight"></param>
    /// <param name="adjust"></param>
    /// <returns></returns>
    public static void GetThumbnailImage(this SixLabors.ImageSharp.Image image, int spcWidth, int spcHeight, bool adjust = true)
    {
        if (spcWidth == 0) spcWidth = image.Width;
        if (spcHeight == 0) spcHeight = image.Height;
        if (adjust)
        {
            var size = AdjustSize(spcWidth, spcHeight, image.Width, image.Height);

             image.Mutate(x => x
                        .Resize(new ResizeOptions
                        {
                            Size = size,
                            Mode = ResizeMode.Max
                        }));
        }
        else
        {
            image.Mutate(x => x
                      .Resize(new ResizeOptions
                      {
                          Size = new SixLabors.ImageSharp.Size(spcWidth, spcHeight),
                          Mode = ResizeMode.Max
                      }));
           
        }
      
    }

    /// <summary>
    /// 从base64获取图片
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static SixLabors.ImageSharp.Image FromBase64(string value)
    {
        byte[] buffer = Convert.FromBase64String(value);
        using (MemoryStream ms = new MemoryStream(buffer))
        {
          return SixLabors.ImageSharp.Image.Load(ms);
        }
    }
}
